package com.wbao.dianniu.tabstrip.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.lhh.apst.library.AdvancedPagerSlidingTabStrip;
import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.R;
import com.wbao.dianniu.fragment.LazyLoadBaseFragment;
import com.wbao.dianniu.im.AddFriendActivity;
import com.wbao.dianniu.im.FriendListActivity;
import com.wbao.dianniu.update.ChatUnreadChangeManger;

/**
 * Created by yejianjun on 2018/5/26.
 */

public class ThirdFragment extends LazyLoadBaseFragment implements View.OnClickListener, ChatUnreadChangeManger.IUnreadChangeListener {

    public AdvancedPagerSlidingTabStrip mAPSTS;
    public APSTSViewPager mVP;

    private static final int VIEW_MSG = 1;
    private static final int VIEW_NOTIFY = 0;

    private static final int VIEW_SIZE = 2;

    //    private Fragment msgFragment = null;
    private ConversationFragment conversationFragment;
    private NotifyFragment notifyFragment;

    private FragmentAdapter adapter;
    private TextView friendsTV;
    private TextView addFriendsTV;
    private int notifyUnreadCounts = 0;
    private int charUnreadCounts = 0;

    public static ThirdFragment instance() {
        ThirdFragment view = new ThirdFragment();
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapter = new FragmentAdapter(getChildFragmentManager());
    }

    /*@Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_third_pager, null);
        mAPSTS = (AdvancedPagerSlidingTabStrip) view.findViewById(R.id.thirdview_tabs);
        mVP = (APSTSViewPager) view.findViewById(R.id.thirdview_apsts);
        mVP.setOffscreenPageLimit(VIEW_SIZE);
        mVP.setNoFocus(false);
        mVP.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        mAPSTS.setIndicatorHeight(4);
        mAPSTS.setViewPager(mVP);

        friendsTV = (TextView) view.findViewById(R.id.friends_tv);
        addFriendsTV = (TextView) view.findViewById(R.id.add_friends_tv);
        friendsTV.setOnClickListener(this);
        addFriendsTV.setOnClickListener(this);

        ChatUnreadChangeManger.getInstance().addUnreadChangeListener(this);
        showUnreadCounts(notifyUnreadCounts);
        showChatUnreadCounts(charUnreadCounts);
        return view;
    }*/

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.friends_tv:
                // 通讯录列表
                Intent friendsIntent = new Intent();
                friendsIntent.setClass(getContext(), FriendListActivity.class);
                getContext().startActivity(friendsIntent);
                break;
            case R.id.add_friends_tv:
                // 加好友列表
                Intent addFriendsIntent = new Intent();
                addFriendsIntent.setClass(getContext(), AddFriendActivity.class);
                getContext().startActivity(addFriendsIntent);
                break;

        }
    }

    @Override
    public void unreadChange(int idType) {

    }

    @Override
    public void addAllNotifyUnread(int counts) {
        showUnreadCounts(+counts);
    }

    @Override
    public void minusAllNotifyUnread(int counts) {
        showUnreadCounts(-counts);
    }

    @Override
    public void addAllChatUnread(int counts) {

    }

    @Override
    public void minusAllChatUnread(int counts) {

    }

    @Override
    public void setChatUnread(int counts) {
        charUnreadCounts = counts;
        showChatUnreadCounts(counts);
    }

    @Override
    public void setNofifyUnread(int counts) {
        notifyUnreadCounts = counts;
        showUnreadCounts(0);
    }

    private class FragmentAdapter extends FragmentStatePagerAdapter {
        protected LayoutInflater mInflater;

        public FragmentAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
            mInflater = LayoutInflater.from(getActivity());
        }


        @Override
        public Fragment getItem(int position) {
            // TODO Auto-generated method stub
            if (position >= 0 && position < VIEW_SIZE) {
                switch (position) {
                    case VIEW_MSG:
                        if (null == conversationFragment) {
                            conversationFragment = ConversationFragment.instance();
                        }
                        return conversationFragment;
                    case VIEW_NOTIFY:
                        if (null == notifyFragment)
                            notifyFragment = NotifyFragment.instance();
                        return notifyFragment;
                    default:
                        break;
                }
            }

            return null;
        }

        @Override
        public int getCount() {
            return VIEW_SIZE;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case VIEW_MSG:
                    return getResources().getString(R.string.message);
                case VIEW_NOTIFY:
                    return getResources().getString(R.string.notify);
                default:
                    break;
            }

            return super.getPageTitle(position);
        }

    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("ThirdFragment"); //统计页面，"MainScreen"为页面名称，可自定义
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("ThirdFragment");
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_third_pager;
    }

    @Override
    protected void initView(View view) {
        mAPSTS = (AdvancedPagerSlidingTabStrip) view.findViewById(R.id.thirdview_tabs);
        mVP = (APSTSViewPager) view.findViewById(R.id.thirdview_apsts);
        mVP.setOffscreenPageLimit(VIEW_SIZE);
        mVP.setNoFocus(false);
        mVP.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        mAPSTS.setIndicatorHeight(4);
        mAPSTS.setViewPager(mVP);

        friendsTV = (TextView) view.findViewById(R.id.friends_tv);
        addFriendsTV = (TextView) view.findViewById(R.id.add_friends_tv);
        friendsTV.setOnClickListener(this);
        addFriendsTV.setOnClickListener(this);

        ChatUnreadChangeManger.getInstance().addUnreadChangeListener(this);
        showUnreadCounts(notifyUnreadCounts);
        showChatUnreadCounts(charUnreadCounts);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ChatUnreadChangeManger.getInstance().removeUnreadChangeListener(this);
    }


    private synchronized void showUnreadCounts(int counts) {
        notifyUnreadCounts = notifyUnreadCounts + counts;
        if (notifyUnreadCounts > 99) {
            mAPSTS.showDot(VIEW_NOTIFY, "99+");
        } else if (notifyUnreadCounts > 0) {
            mAPSTS.showDot(VIEW_NOTIFY, String.valueOf(notifyUnreadCounts));
        } else {
            mAPSTS.hideDot(VIEW_NOTIFY);
        }
    }

    private synchronized void showChatUnreadCounts(int counts) {
        charUnreadCounts = counts;
        if (charUnreadCounts > 99) {
            mAPSTS.showDot(VIEW_MSG, "99+");
        } else if (charUnreadCounts > 0) {
            mAPSTS.showDot(VIEW_MSG, String.valueOf(charUnreadCounts));
        } else {
            mAPSTS.hideDot(VIEW_MSG);
        }
    }

}